﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace ClipboardManager
{
    class Helper
    {
        private const int sDefaultFontSize = 12;
        private const int sMaxFontSize = 14;
        private const int sMinFontSize = 8;


        private Helper()
        {
        }

        public static string ArrToList(string[] strings)
        {
            StringBuilder sb = new StringBuilder();

            foreach (string i in strings)
            {
                sb.AppendLine(i);
            }
            return sb.ToString();
        }

        public static string ArrToString(string[] strings)
        {
            StringBuilder sb = new StringBuilder();

            foreach (string i in strings)
            {
                sb.Append(i);
                sb.Append(", ");
            }
            return sb.ToString();
        }

        public static int CountOccurrences(string text, string pattern)
        {
            int occurencies = 0;
            int position = 0;

            while ((position = text.IndexOf(pattern, position)) != -1)
            {
                position += pattern.Length;
                occurencies++;
            }
            return occurencies;
        }

        public static string TextToRtf(string text)
        {
            System.Windows.Forms.RichTextBox textBox = new System.Windows.Forms.RichTextBox();
            textBox.Text = text;
            return textBox.Rtf;
        }

        public static void Msg(string text)
        {
            MainClass.Log(text);
        }

        public static string TrimRtf(string text)
        {
            if (string.IsNullOrEmpty(text))
                return "";

            RichTextBox textBox = new RichTextBox();
            textBox.Rtf = text;

            int rowIndex = textBox.Text.IndexOf("\n\n");
            while (rowIndex > 0)
            {
                textBox.Select(rowIndex, 2);
                textBox.SelectedText = "\n";
                rowIndex = textBox.Text.IndexOf("\n\n");
            }

            rowIndex = textBox.Text.IndexOf("\r\n");
            while (rowIndex > 0)
            {
                textBox.Select(rowIndex, 2);
                textBox.SelectedText = "\n";
                rowIndex = textBox.Text.IndexOf("\r\n");
            }

            rowIndex = textBox.Text.IndexOf("   ");
            while (rowIndex > 0)
            {
                textBox.Select(rowIndex, 2);
                textBox.SelectedText = " ";
                rowIndex = textBox.Text.IndexOf("   ");
            }

            while (textBox.Text.StartsWith(" ") || textBox.Text.StartsWith("\n") || textBox.Text.StartsWith("\r"))
            {
                textBox.Select(0, 1);
                textBox.SelectedText = "";
            }

            while (textBox.Text.EndsWith(" ") || textBox.Text.EndsWith("\n") || textBox.Text.EndsWith("\r"))
            {
                textBox.Select(textBox.Text.Length - 1, 1);
                textBox.SelectedText = "";
            }

            textBox.SelectAll();

            if (textBox.SelectionFont.Size > sMaxFontSize || textBox.SelectionFont.Size < sMinFontSize)
            {
                textBox.SelectionFont = new Font(
                    textBox.SelectionFont.Name,
                    sMaxFontSize,
                    textBox.SelectionFont.Style,
                    textBox.SelectionFont.Unit,
                    textBox.SelectionFont.GdiCharSet,
                    textBox.SelectionFont.GdiVerticalFont);
            }

            textBox.Select(0, 0);

            string returnString = textBox.Rtf;
            textBox.Dispose();
            textBox = null;
            return returnString;
        }

        public static string Trim(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";

            char[] text = value.ToCharArray();
            char[] result = new char[text.Length];
            short Length = 0;
            char last = ' ';

            for (int i = 0; i < text.Length; i++)
			{
                if (text[i] == '\t')
                    continue;

			    if (last != text[i])
                {
                    result[Length] = text[i];
                    Length++;
                    last = text[i];
                }
                    
			}

            return new string(result,0,(int)Length);
        }

    }

    /// <summary>
    /// Log the time to complete something and display it in the output
    /// </summary>
    class TimeLog
    {
        private Stopwatch stopWatch = new Stopwatch();
        private string LogName = "NoName";
        bool IsHandled = false;

        /// <summary>
        /// Make a new logTime with the name "NoName"
        /// </summary>
        public TimeLog()
        {
            stopWatch.Start();
        }

        /// <summary>
        /// Make a new TimeLog and start the timer
        /// </summary>
        /// <param name="name">The name of the object thats beeing logged</param>
        public TimeLog(string name)
        {
            stopWatch.Start();
            LogName = name;
        }

        /// <summary>
        /// Call when task is complete then dispose object
        /// </summary>
        public void EndLog()
        {
            MainClass.Log(LogName + " took " + stopWatch.ElapsedTicks.ToString() + " ticks to complete");
            IsHandled = true;
        }

        /// <summary>
        /// Create a new log and write out the old one if it has not been handled
        /// </summary>
        /// <param name="name">The name of the object thats beeing logged</param>
        public void NewLog(string name)
        {
            if (!IsHandled)
            {
                EndLog();
            }
            stopWatch.Start();
            LogName = name;
        }
    }
}
